package com.conferenceroom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.conferenceroom.constant.MessageConstant;
import com.conferenceroom.entity.User;
import com.conferenceroom.mapper.UserMapper;
import com.conferenceroom.service.ILoginService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class LoginServiceImpl implements ILoginService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public User doLogin(User user) {

        if (ObjectUtils.isEmpty(user)) {
            return null;
        }
        if (!StringUtils.hasLength(user.getUsername())){
            return null;
        }
        if (!StringUtils.hasLength(user.getPassword())) {
            return null;
        }
        List<User> users = new ArrayList<>();
        users = userMapper.selectByUsername(user.getUsername());
        if(users == null||users.isEmpty()) return null;
        if (!users.isEmpty()) {
            User bean = users.get(0);
            // 新增状态检查
            if (bean.getStatus() == 2) { // 2表示待审核状态
                return null;
            }
            // 新增状态检查
            if (bean.getStatus() == 0) { // 0表示已冻结
                return null;
            }
            if (ObjectUtils.isEmpty(bean)) {
                return null;
            }
            if (user.getPassword().equals(bean.getPassword())) {
                return bean;
            }
        }
        return null;
    }

    @Override
    public String doRegister(User user) {
        if (ObjectUtils.isEmpty(user)) {
            return MessageConstant.USER_EMPTY;
        }
        if (!StringUtils.hasLength(user.getUsername())){
            return MessageConstant.USERNAME_EMPTY;
        }
        if (!StringUtils.hasLength(user.getRealName())){
            return MessageConstant.REALNAME_EMPTY;
        }
        if (!StringUtils.hasLength(user.getPassword())) {
            return MessageConstant.PASSWORD_EMPTY;
        }
        if (!StringUtils.hasLength(user.getCompany())) {
            user.setCompany("无公司");
        }
        if (user.getUserType() == 0)
        {
            QueryWrapper<User> queryUsername = new QueryWrapper<>();
            QueryWrapper<User> queryPhone = new QueryWrapper<>();
            queryUsername.eq("username", user.getUsername()).eq("user_type",0);
            queryPhone.eq("phone", user.getPhone()).eq("user_type",0);
            if(!userMapper.selectList(queryUsername).isEmpty())
            {
                return MessageConstant.USERNAME_DUPLICATE;
            }
            if (!userMapper.selectList(queryPhone).isEmpty()) {
                return MessageConstant.PHONE_DUPLICATE;
            }
            user.setUserType(0);
            user.setStatus(2);//待审核
            userMapper.insert(user);
            return MessageConstant.REGISTER_USER_VERIFY;
        }else if(user.getUserType() == 1)
        {
            QueryWrapper<User> queryUsername = new QueryWrapper<>();
            QueryWrapper<User> queryPhone = new QueryWrapper<>();
            queryUsername.eq("username", user.getUsername()).eq("user_type",1);
            queryPhone.eq("phone", user.getPhone()).eq("user_type",1);
            if(!userMapper.selectList(queryUsername).isEmpty())
            {
                return MessageConstant.USERNAME_DUPLICATE;
            }
            if (!userMapper.selectList(queryPhone).isEmpty()) {
                return MessageConstant.PHONE_DUPLICATE;
            }
            user.setUserType(1);
            user.setStatus(2);//待审核
            userMapper.insert(user);
            return MessageConstant.REGISTER_EMPLOYEE_VERIFY;
        }
        return MessageConstant.UNKNOWN_ERROR;
    }
}
